Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#639 Add Image model, update related code #646

Merged
merged 42 commits into from
Jun 26, 2024

Conversation

Lvyshnevska
Copy link
Collaborator

ProfileImage model has been created. Views, serializers for images and all the related code has been updated.

@Lvyshnevska Lvyshnevska linked an issue Jun 20, 2024 that may be closed by this pull request
context = super().get_serializer_context()
image_type = self.kwargs.get("image_type")
context.update({"image_type": image_type})
return context
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there is no need to do this.

In Serializer code, you can do self.context['view'].kwargs['image_type'].

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

content_type=content_type,
hash_md5=hash_md5,
image_size=image_size,
created_by=user,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use unnecessary variables.

created_by=self.request.user

↑ is much cleaner than to scan the code above to find out, what exactly we put into user variable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the same for image_size and image_type (which you for some reason pass also through serializer context?..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary variables removed

)
logo_approved_image = serializers.ImageField(
source="logo_approved.image_path", required=False
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In publicly (i.e. not admin-only) avaialbe API there must be only banner and logo fields. Which return approved images.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done
Custom ProfileImage field that contain uuid and path fields created. In publicly API only banner and logo fields are available that return approved or last uploaded (for the profile owner) images.

raise ValidationError(
"Unsupported image format. Only PNG and JPEG are allowed."
)
with Image.open(image) as img:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking filename extension will be much faster.

validate_banner_size(image)
else:
validate_logo_size(image)
return value
Copy link

@ProDG ProDG Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk about if/else here. When you'll add some third data type — chances you'll remember to add it here are pretty slim (especially if it will be OK to use validate_logo_size()).

validator_function = {
    ProfileImage.BANNER: validate_banner_size,
    ProfileImage.LOGO: validate_logo_size,
}[self.context["view"].kwargs["image_type"]
validator_function(value.get("image_path"))

return value

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return None

def to_internal_value(self, data):
profile_image = ProfileImage.objects.filter(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

value.image_path.url
),
}
return None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None is returned by default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

),
"rb",
self.banner_path = os.path.join(
os.getcwd(), "images", "tests", "img", "img_2mb.png"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it will be more readable:
os.path.join( os.getcwd(), "images/tests/img/img_2mb.png")

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not sure if it will work on Windows, if no — return to the curren option).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

profile_image = ProfileImage.objects.filter(
uuid=data, is_deleted=False
).first()
return profile_image
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, you can remove some redundant code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Lvyshnevska Lvyshnevska merged commit b8140a0 into develop Jun 26, 2024
4 checks passed
@Lvyshnevska Lvyshnevska deleted the #639-AddImageModelUpdateRelatedCode branch June 26, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Image model, update related code
4 participants